package com.xxx.common.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxx.common.entity.User;
import com.xxx.common.mapper.UserMapper;
import com.xxx.common.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(String userName, String password) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",userName).eq("password",password);
        User user = userMapper.selectOne(wrapper);
        return user;
    }

    @Override
    public PageInfo<User> selectUserInfo(User user, Integer pageNum, Integer pageSize) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        PageHelper.startPage(pageNum,pageSize);
        if (user.getRealName() !=null){
            wrapper.like("real_name",user.getRealName());
        }
        if (user.getUserType() !=null){
            wrapper.eq("user_type",user.getUserType());
        }
        if (user.getId() !=null){
            wrapper.eq("id",user.getId());
        }
        List<User> data = userMapper.selectList(wrapper);
        return new PageInfo<>(data);
    }

    @Override
    public Integer saveAndUpdateUserInfo(User user) {
        Integer count;
        if (user.getId() == null){
            count = userMapper.insert(user);
        }else {
            count = userMapper.updateById(user);
        }
        return count;
    }

    @Override
    public Integer delUserInfo(Integer id) {
        return userMapper.deleteById(id);
    }

    @Override
    public User selectUserInfoById(Integer id) {
        return userMapper.selectById(id);
    }
}
